package com.good.child.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.bean.StudyGardenBean;
import com.good.child.bean.constants.ConstantsBean;
import com.good.child.http.StudyGardenLogical;
import com.good.child.iview.StudyGardenIView;
import com.good.child.pressent.StudyGardenPresenter;
import com.good.child.widgets.pinyin.PinyinTextView;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.tool.TActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create on: 2024/10/18
 * by Mr.Zhu
 * Description: TODO
 */
public class StudyGardenActivity extends BaseActivity<StudyGardenIView, StudyGardenPresenter> implements StudyGardenIView{

    @BindView(R.id.study_original_tv)
    TextView originalTv;
    @BindView(R.id.study_translation_tv)
    TextView translationTv;
    @BindView(R.id.study_appreciating_tv)
    TextView appreciatingTv;
    @BindView(R.id.study_next_tv)
    TextView nextTv;
    @BindView(R.id.study_last_tv)
    TextView lastTv;
    @BindView(R.id.study_open_image)
    ImageView openImage;

    @BindView(R.id.study_title)
    PinyinTextView titlePinyin;
    @BindView(R.id.study_author)
    PinyinTextView authorPinyin;
    @BindView(R.id.study_content)
    PinyinTextView contentPinyin;
    @BindView(R.id.study_content_small)
    PinyinTextView contentSmallPinyin;

    private StudyGardenBean currentBean;
    private int pageNum = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_study_graden);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public void onSuccess(List<ConstantsBean> dataList) {

    }

    @Override
    protected StudyGardenPresenter initPresenter() {
        return new StudyGardenPresenter(this);
    }

    @Override
    protected void initView() {
        setStatusBar(true, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setViewLineVisable(false);
        setTitle("学习园地");
        openImage.postDelayed(new Runnable() {
            @Override
            public void run() {
                getStudyContent();
            }
        },300);

    }

    private void getStudyContent() {
        StudyGardenLogical.getInstance().requestStudyList(this, pageNum+"", "1", new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                currentBean = (StudyGardenBean) result;
                if (currentBean.rows != null) {
                    titlePinyin.setPinyinText(initPinyinTitleData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
                    authorPinyin.setPinyinText(initPinyinAuthorData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
                    contentPinyin.setPinyinText(initPinyinContentData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
                }
                resetNextStatus();
            }
        });
    }

    private void resetNextStatus() {
        if (pageNum == 1) {
            lastTv.setVisibility(View.GONE);
            nextTv.setVisibility(View.VISIBLE);
        } else if (pageNum == currentBean.total) {
            lastTv.setVisibility(View.VISIBLE);
            nextTv.setVisibility(View.GONE);
        } else {
            lastTv.setVisibility(View.VISIBLE);
            nextTv.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.study_original_tv, R.id.study_translation_tv, R.id.study_appreciating_tv,R.id.study_next_tv,R.id.study_last_tv})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.study_original_tv:
                setOriginal(0);
                break;
            case R.id.study_translation_tv:
                setOriginal(1);
                break;
            case R.id.study_appreciating_tv:
                setOriginal(2);
                break;
            case R.id.study_next_tv:
                pageNum++;
                if (currentBean != null && pageNum > currentBean.total) {
                    pageNum = currentBean.total;
                }
                getStudyContent();
                break;
            case R.id.study_last_tv:
                pageNum--;
                if (pageNum < 1) {
                    pageNum = 1;
                }
                getStudyContent();
                break;
        }
    }

    private void setOriginal(int status) {
        if (status == 0) {//原文
            originalTv.setBackground(getResources().getDrawable(R.drawable.shape_ff0e52_to_ff4498_45));
            originalTv.setTextColor(getResources().getColor(R.color.white));
            translationTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            translationTv.setTextColor(getResources().getColor(R.color.black));
            appreciatingTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            appreciatingTv.setTextColor(getResources().getColor(R.color.black));
            contentPinyin.setPinyinText(initPinyinContentData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
//            contentPinyin.setHorizontalSpacing(DimensionUtil.dpToPx(40));
            contentPinyin.setVisibility(View.VISIBLE);
            contentSmallPinyin.setVisibility(View.GONE);
        } else if (status == 1) {//译文
            translationTv.setBackground(getResources().getDrawable(R.drawable.shape_ff0e52_to_ff4498_45));
            translationTv.setTextColor(getResources().getColor(R.color.white));
            originalTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            originalTv.setTextColor(getResources().getColor(R.color.black));
            appreciatingTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            appreciatingTv.setTextColor(getResources().getColor(R.color.black));
            contentSmallPinyin.setPinyinText(initPinyinContentTranslationData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
            contentPinyin.setVisibility(View.GONE);
            contentSmallPinyin.setVisibility(View.VISIBLE);
        } else {//欣赏
            appreciatingTv.setBackground(getResources().getDrawable(R.drawable.shape_ff0e52_to_ff4498_45));
            appreciatingTv.setTextColor(getResources().getColor(R.color.white));
            originalTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            originalTv.setTextColor(getResources().getColor(R.color.black));
            translationTv.setBackground(getResources().getDrawable(R.drawable.shape_ffe0ee_45));
            translationTv.setTextColor(getResources().getColor(R.color.black));
            contentSmallPinyin.setPinyinText(initPinyinContentAppreciatingData(currentBean),PinyinTextView.TYPE_PINYIN_AND_TEXT);
            contentPinyin.setVisibility(View.GONE);
            contentSmallPinyin.setVisibility(View.VISIBLE);
        }
    }

    private List<Pair<String, String>> initPinyinData() {
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        pinyinList.add(Pair.create("这", "zhè"));
        pinyinList.add(Pair.create("是", "shì"));
        pinyinList.add(Pair.create("一个", "yī gè"));
        pinyinList.add(Pair.create("拼音", "pīn yīn"));
        pinyinList.add(Pair.create("组件", "zǔ jiàn"));
        return pinyinList;
    }

    private List<Pair<String, String>> initPinyinTitleData(StudyGardenBean studyGardenBean){
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        if (studyGardenBean.rows.isEmpty()) {
            return pinyinList;
        }
        StudyGardenBean gardenBean = studyGardenBean.rows.get(0);
        if (TextUtils.isEmpty(gardenBean.getTitle())) {
            return pinyinList;
        }
        String[] input = gardenBean.getTitle().split(" ");
        String[] pinyin  =gardenBean.getTitlePy().split(" ");
        for (int i = 0; i < input.length;i++) {
            pinyinList.add(Pair.create(input[i], pinyin[i]));
        }
        return pinyinList;
    }

    private List<Pair<String, String>> initPinyinAuthorData(StudyGardenBean studyGardenBean){
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        if (studyGardenBean.rows.isEmpty()) {
            return pinyinList;
        }
        StudyGardenBean gardenBean = studyGardenBean.rows.get(0);
        if (TextUtils.isEmpty(gardenBean.getAuther())) {
            return pinyinList;
        }
        String[] input = gardenBean.getAuther().split(" ");
        String[] pinyin  =gardenBean.getAutherPy().split(" ");
        for (int i = 0; i < input.length;i++) {
            if (i < pinyin.length) {
                pinyinList.add(Pair.create(input[i], pinyin[i]));
            } else {
                pinyinList.add(Pair.create(input[i], ""));
            }
        }
        return pinyinList;
    }

    /**
     * 原文
     * @param studyGardenBean
     * @return
     */
    private List<Pair<String, String>> initPinyinContentData(StudyGardenBean studyGardenBean){
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        if (studyGardenBean.rows.isEmpty()) {
            return pinyinList;
        }
        StudyGardenBean gardenBean = studyGardenBean.rows.get(0);
        if (TextUtils.isEmpty(gardenBean.getContent())) {
            return pinyinList;
        }
        String[] input = gardenBean.getContent().trim().replace("-","").split(" ");
        String[] pinyin  =gardenBean.getContentPy().trim().replace("-","").split(" ");
        for (int i = 0; i < input.length;i++) {
            if (i < pinyin.length) {
                pinyinList.add(Pair.create(input[i], pinyin[i]));
            } else {
                pinyinList.add(Pair.create(input[i], ""));
            }
        }
        return pinyinList;
    }

    /**
     * 原文
     * @param studyGardenBean
     * @return
     */
    private List<Pair<String, String>> initPinyinContentTranslationData(StudyGardenBean studyGardenBean){
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        if (studyGardenBean.rows.isEmpty()) {
            return pinyinList;
        }
        StudyGardenBean gardenBean = studyGardenBean.rows.get(0);
        if (TextUtils.isEmpty(gardenBean.getTranslation())) {
            return pinyinList;
        }
        String[] input = gardenBean.getTranslation().trim().split("(?<!^)(?!$)");
//        String[] pinyin  =studyGardenBean.getContentPy().trim().replace("-","").split(" ");
        for (int i = 0; i < input.length;i++) {
            pinyinList.add(Pair.create(input[i], ""));
        }
        return pinyinList;
    }

    /**
     * 原文
     * @param studyGardenBean
     * @return
     */
    private List<Pair<String, String>> initPinyinContentAppreciatingData(StudyGardenBean studyGardenBean){
        List<Pair<String, String>> pinyinList = new ArrayList<>();
        if (studyGardenBean.rows.isEmpty()) {
            return pinyinList;
        }
        StudyGardenBean gardenBean = studyGardenBean.rows.get(0);
        if (TextUtils.isEmpty(gardenBean.getAppreciation())) {
            return pinyinList;
        }
        String[] input = gardenBean.getAppreciation().trim().split("(?<!^)(?!$)");
        for (int i = 0; i < input.length;i++) {
            pinyinList.add(Pair.create(input[i], ""));
        }
        return pinyinList;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
